#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    extern char** environ;

    pid_t id = fork();
    
    if (id == 0)
    {
        printf("begin running pid: %d, ppid: %d\n", getpid(), getppid());
        
        char* my_env[] = { "VALUE=111", "USER=MY", "PWD=./XXX", NULL };

        //execl("/usr/bin/ls", "ls", "-l", "-a", NULL);
        //execlp("ls", "ls", "-l", "-s", NULL);
        //execle("./other", "other", "-abc", "-ABC", "-123", NULL, environ);

        char* my_argv[] = { "other", "-abc", "-ABC", "-123", NULL };

        //execv("./other", my_argv);
        //execvp("./other", my_argv);
        //execvpe("./other", my_argv, my_env);
        //execve("./other", my_argv, my_env);
        
        printf("runn end pid: %d, ppid: %d\n", getpid(), getppid());
        exit(1);
    }
    else if (id > 0)
    {
        int state = 0;
        pid_t ret = waitpid(id, &state, 0);
        if (ret == id)
        {
            printf("wait id = %d\nexit code: %d\n", ret, WEXITSTATUS(state));
            if (!WIFEXITED(state))
                printf("sig code: %d\n", state & 0x7F);
        }
        else
            printf("wait error\n");
    }
    else
        printf("fork error\n");

    return 0;
}
